package com.hly.utils;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.logging.SimpleFormatter;

public class PushApi {


    /**
     * 如果有通知时，展示相应信息：
     * 高亮红点：展示未读消息数量；
     *
     * 时间（内页同理）：
     * 展示最新一条消息的时间，精确到分钟。
     * 今天：只展示时间（如：16:52）
     * 昨天：展示“昨日”+“时间”（如：昨日 16:52）
     * 昨天以前，今年以内：展示“月/日”+“时间”（如：01/03 16:52）
     * 今年以前：展示“年/月/日”+“时间”（如：2023/12/30 16:52）
     *
     * 内容：
     * 展示最近一条消息的内容缩略；@曾涛提供
     * @param args
     */
    public static void main(String[] args) {
//        push();
//        long timestamp = 1644151928000L;
        long timestamp = 1679526600000L;//2024/3/27 14:10
        //根据2024/3/28 14:10输出龙型的时间戳
//        AA – 前导帧(preamble)
//        0x50655DAB – 访问地址(access address)
//        1E – LL帧头字段(LL header)
//        08 – 有效数据包长度(payload length)
//        04000400 – ATT数据长度，以及L2CAP通道编号
//        1B – notify command
//        0x0013 – 电量数据handle
//        0x53 – 真正要发送的电量数据
//        0xF650D5 – CRC24值
        System.out.println("AAAB5D65501E08040004001B130053D550F6".length());
        System.out.println(230000L*50000L);
    }


    static String getMid(String mid) {
        if (null == mid || "".equals(mid)) {
            return "";
        }
        if (mid.contains("&") && mid.contains("=")) {
            return mid.split("&")[0].split("=")[1];
        } else if (!mid.contains("&") && !mid.contains("=")) {
            return mid;
        } else if (mid.contains("=")) {
            return mid.split("=")[1];
        } else {
            return "";
        }
    }


    private static void push() {
        try {
//            URL url = new URL("https://push.api.engagelab.cc");
            URL url = new URL("https://push.api.engagelab.cc/v4/push");
            byte[] bytes = Files.readAllBytes(Paths.get("D:\\JavaProject\\string-parse\\src\\com\\hly\\utils\\Main\\api.json"));
            String json = new String(bytes);
            System.out.println(json);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestMethod("GET");
            httpConn.setRequestProperty("Content-Type", "application/json");
            byte[] message = ("d72bff6e875ddb050e74b781:af9d9de5d296278f4ec3e549").getBytes("UTF-8");
            String basicAuth = DatatypeConverter.printBase64Binary(message);
            System.out.println(basicAuth);
//            httpConn.setRequestProperty("Authorization", "Basic ZDcyYmZmNmU4NzVkZGIwNTBlNzRiNzgxOmFmOWQ5ZGU1ZDI5NjI3OGY0ZWMzZTU0OQ==");
            httpConn.setRequestProperty("Authorization", "Basic " + basicAuth);
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
            writer.write(json);
            writer.flush();
            writer.close();
            httpConn.getOutputStream().close();

            InputStream responseStream = httpConn.getResponseCode() / 100 == 2 ? httpConn.getInputStream() : httpConn.getErrorStream();
            Scanner s = new Scanner(responseStream).useDelimiter("\\A");
            String response = s.hasNext() ? s.next() : "";
            System.out.println(response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
